<?php
class Property extends AppModel {
    var $name = 'Property';
    var $primaryKey = 'property_id';
    var $displayField = 'flat';

    var $hasOne = array(
        'Account'
    );

    var $belongsTo = array(
        'Building',
        'PropertyType',
        'Owner' => array(
                'className' => 'Person',
                'foreignKey' => 'owner_id'
        ),
        'OwnerRepresentative' => array(
                'className' => 'Person',
                'foreignKey' => 'owner_representative_id'
        ),
        'Tenant' => array(
                'className' => 'Person',
                'foreignKey' => 'tenant_id'
        ),
        'TenantRepresentative' => array(
                'className' => 'Person',
                'foreignKey' => 'tenant_representative_id'
        )
    );

    var $hasMany = array(
        'Expense',
    );
	
	// Trae el id del que esta ocupando la propiedad.
	// el que sea distinto de null es el que va
	function get_occupant ($id)
    {   
        $property =  $this->read(null,$id);
	    $occupant = $property['Property']['owner_id'];
		if ( $property['Property']['tenant_id'] != null ){
	    	$occupant = $property['Property']['tenant_id'];
		}else{
			if ( $property['Property']['owner_representative_id'] != null ){
	    		$occupant = $property['Property']['owner_representative_id'];
			}else{
				if ( $property['Property']['tenant_representative_id'] != null ){
					$occupant = $property['Property']['tenant_representative_id'];
				}
			}
		}
		return ($occupant);	
   }  

}
?>
